﻿using System;
using System.Linq;
using System.Xml.Serialization;

namespace Altairis.OtpAuth.Core {
    [XmlRoot(ElementName = "eventOtpGenerator", Namespace = XmlNs.SERIALIZATION_NS_URL)]
    public class EventOtpGenerator : OtpGenerator {
        // Default values set by RFC 4226
        private const int DEFAULT_DIGITS = 6;
        private const bool DEFAULT_USE_CHECKSUM = false;

        // Constructors

        public EventOtpGenerator()
            : base(DEFAULT_HMAC_ALGORITHM_NAME) {
            // Initialize default options per RFC
            this.Digits = DEFAULT_DIGITS;
            this.UseChecksumDigit = DEFAULT_USE_CHECKSUM;
        }

        // Methods

        public string GenerateOneTimePassword(byte[] secret, long counter) {
            if (secret == null || secret.Length == 0) throw new ArgumentException("Value cannot be null or empty.", "secret");
            if (counter < 0) throw new ArgumentOutOfRangeException("counter");

            var movingFactor = BitConverter.GetBytes(counter).Reverse().ToArray();
            return base.GenerateOneTimePassword(secret, movingFactor);
        }

    }
}
